package controller.alarm;

import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import layout.alarm.AlarmAdapter;
import layout.alarm.CustomItemAlarm;
import object.alarm.Alarm;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import database.alarm.AlarmDelegate;

public class AlarmActivity extends ListActivity implements OnClickListener{
	/** Called when the activity is first created. */
	private static final int ENABLE_ALL = Menu.FIRST;
    private static final int DISABLE_ALL = Menu.FIRST + 2;
    
    private Vector<Alarm> listAlarms;
    private AlarmAdapter alarmAdapter;
    public static AlarmDelegate dataSource;
    private Button btnAdd;
    private Button btnDel;
    private Button btnSetting;
    private TextView date;
    
    public static HashMap<Integer, AlarmManager> listAlarmManager = new HashMap<Integer, AlarmManager>();
    public static HashMap<Integer, PendingIntent> listPI = new HashMap<Integer, PendingIntent>();
    public static Vibrator vibrator;
    
    private int itemSelected = -1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
        
        date = (TextView) findViewById(R.id.date);
        date.setText(DateFormat.format("E, MMMM dd, yyyy", new Date()));
        
        dataSource = new AlarmDelegate(this);
        dataSource.open();
        listAlarms = dataSource.getAllAlarm();
        alarmAdapter = new AlarmAdapter(this.getApplicationContext(), listAlarms);
        setListAdapter(alarmAdapter);
        
        btnAdd = (Button) findViewById(R.id.btnAdd); 
        btnAdd.setOnClickListener(this);
        
        btnDel = (Button) findViewById(R.id.btnDel);
        btnDel.setOnClickListener(this);
        
        btnSetting = (Button) findViewById(R.id.btnSetting);
        btnSetting.setOnClickListener(this);
        
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);        
        menu.add(0, ENABLE_ALL, 0,"Enable all" ).setIcon(android.R.drawable.ic_menu_info_details);      
        menu.add(0, DISABLE_ALL, 0,"Disable all" ).setIcon(android.R.drawable.ic_menu_info_details);
        return true;
    }
    
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
			case ENABLE_ALL:
				dataSource.editStatusAllAlarm(true);
				for(int i=0; i<listAlarms.size(); i++){
					CustomItemAlarm current = (CustomItemAlarm)getListView().getChildAt(i);
					current.setSelected(true);
				}
				alarmAdapter.notifyDataSetChanged();
				break;
			case DISABLE_ALL:
				dataSource.editStatusAllAlarm(false);
				for(int i=0; i<listAlarms.size(); i++){
					CustomItemAlarm current = (CustomItemAlarm)getListView().getChildAt(i);
					current.setSelected(false);
				}
				alarmAdapter.notifyDataSetChanged();
				break;
		}
		return true;
	}

	public void onClick(View v) {
		if(v == btnAdd){
			vibrator.vibrate(500);
			Intent intent = new Intent(AlarmActivity.this, AddAlarm.class);
			AlarmActivity.this.startActivity(intent);
		}else if(v == btnDel){
			if(itemSelected==-1) return;
			else{
				boolean flag = dataSource.deleteAlarm(listAlarms.get(itemSelected).getId());
				if(flag){
					listAlarms.remove(itemSelected);
					alarmAdapter.notifyDataSetChanged();
//					listAlarmManager.get(idItem).cancel(listPI.get(idItem));
//					listPI.remove(idItem);
//					listAlarmManager.remove(idItem);
				}else{
					Toast toast = Toast.makeText(getApplicationContext(), "Error! Try again.", 1000);
					toast.show();
					return;
				}
			}
		}else if(v == btnSetting){
//			Intent intent = new Intent(AlarmActivity.this, MusicListActivity.class);
//			AlarmActivity.this.startActivity(intent);
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		itemSelected = position;
	}

	@Override
	protected void onDestroy() {
		dataSource.close();
		super.onDestroy();
	}

	@Override
	protected void onRestart() {
		dataSource.open();
		super.onRestart();
	}

	@Override
	protected void onResume() {
		dataSource.open();
		super.onResume();
	}
}